using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using bitzhuwei.Compiler;

namespace bitzhuwei.AnsiCFormat {
    /// <summary>
    /// Correspond to the Vn node logical_and_expression in the grammar(AnsiC).
    /// </summary>
    internal partial class Vnlogical_and_expression : IFullFormatter {
        // [052]=logical_and_expression : inclusive_or_expression ;
        // [053]=logical_and_expression : logical_and_expression '&&' inclusive_or_expression ;
        private readonly Vninclusive_or_expression first0;
        private readonly List<Token> list0 = new();
        private readonly List<Vninclusive_or_expression> list1 = new();
        private readonly TokenRange _scope;
        public TokenRange Scope => _scope;

        public void Format(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }

        public void FullFormat(BlankConfig preConfig, TextWriter writer, FormatContext context) {
            {
                this.first0.FullFormat(preConfig, writer, context);
            }
            var config = new BlankConfig(inlineBlank: 1, forceNewline: false);
            var first = true;
            for (var i = 0; i < this.list0.Count; i++) {
                var op = this.list0[i];
                if (first) {
                    context.PrintCommentsBetween(this.first0, op, config, writer);
                    first = false;
                }
                else {
                    context.PrintCommentsBetween(this.list1[i - 1], op, config, writer);
                }
                context.PrintBlanksAnd(op, config, writer);
                var exp = this.list1[i];
                context.PrintCommentsBetween(op, exp, config, writer);
                context.PrintBlanksAnd(exp, config, writer);
            }
        }

        public IEnumerable<string> YieldTokens(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }

        // [52]=logical_and_expression : inclusive_or_expression ;
        // dumped by ListExtractor 1
        internal Vnlogical_and_expression(Vninclusive_or_expression first0) {
            this.first0 = first0;
            this._scope = new TokenRange(first0);
        }
        // [53]=logical_and_expression : logical_and_expression '&&' inclusive_or_expression ;
        // dumped by ListExtractor 2
        internal void Add(
            Token r1/* reserved word is omissible */
            , Vninclusive_or_expression r0
        ) {
            this.list0.Add(r1);
            this.list1.Add(r0);
            this._scope.end = r0.Scope.end;
        }

    }
}
